package com.lanqiao.attendance.service.impl;

import com.lanqiao.attendance.mapper.AttendanceMapper;
import com.lanqiao.attendance.mapper.CourseMapper;
import com.lanqiao.attendance.mapper.StudentMapper;
import com.lanqiao.attendance.pojo.Attendance;
import com.lanqiao.attendance.pojo.Course;
import com.lanqiao.attendance.pojo.Student;
import com.lanqiao.attendance.service.AttendanceService;
import com.lanqiao.attendance.vo.AttendanceVo;
import com.lanqiao.attendance.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    AttendanceMapper attendanceMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    CourseMapper courseMapper;


    @Override
    public Result queryById(Long id) {
        Attendance attendance =  attendanceMapper.queryById(id);
        if (attendance != null) {
            return Result.success("查询签到信息成功",attendance);
        } else {
            return Result.fail(3012,"查询签到信息失败");
        }
    }

    @Override
    public Result findById(Long stuId, Long courseId) {
        Attendance attendance = attendanceMapper.findById(stuId, courseId);
        if (attendance != null) {
            return Result.success("根据学号和课程号查询签到信息成功",attendance);
        } else {
            return Result.fail(3013,"根据学号和课程号查询签到信息失败");
        }
    }

    @Override
    public Result queryAll() {
        List<Attendance> attendanceList =  attendanceMapper.queryAll();
        if (attendanceList.size() > 0) {
            return Result.success("查询所有签到信息成功",attendanceList);
        } else {
            return Result.fail(3006,"查询所有签到信息失败");
        }
    }

    @Override
    public Result queryByCourseNum(Long courseId) {
        List<Attendance> attendanceList = attendanceMapper.queryByCourseNum(courseId);
        if (attendanceList.size() > 0) {
            return Result.success("查询签到列表成功",attendanceList);
        } else {
            return Result.fail(3007,"查询签到列表失败");
        }
    }


    @Transactional
    @Override
    public Result insert(Attendance attendance) {
        System.out.println(attendance);
        int row = 0;
        Long stuId = attendance.getStuId();
        Long courseId = attendance.getCourseId();
        Student student = studentMapper.queryStuById(stuId);
        Course course = courseMapper.queryById(courseId);
        if (student != null && course != null) {
            row = attendanceMapper.update(attendance.getStuId(),attendance.getCourseId());
        }

        if (row > 0) {
            Attendance newAttendance = attendanceMapper.queryById(attendance.getId());
            return Result.success("插入签到信息成功",newAttendance);
        } else {
            return Result.fail(3008,"插入签到信息失败");
        }
    }

    @Override
    public Result update(Long stuId, Long courseId) {
        int row = attendanceMapper.update(stuId, courseId);
        if (row > 0) {
            Attendance updateAttendance = attendanceMapper.findById(stuId, courseId);
            return Result.success("更新签到信息成功",updateAttendance);
        } else {
            return Result.fail(3009,"更新签到信息失败");
        }
    }

    @Override
    public Result delete(Long stuId, Long courseId) {
        int row = attendanceMapper.delete(stuId, courseId);
        if (row > 0) {
            Attendance deleteAttendance = attendanceMapper.findById(stuId,courseId);
            return Result.success("删除签到信息成功",deleteAttendance);
        } else {
            return Result.fail(3010,"删除签到信息失败");
        }
    }

    @Override
    public Result attendanceCount(Long courseId, String status) {
        String msg = "";
        int count = attendanceMapper.attendanceCount(courseId, status);
        if (count > 0) {
            if (status == "0") {
                msg = "查询未签到人数成功";
            } else if (status == "1") {
                msg = "查询已签到人数成功";
            }
        }

        if (status != "0" && status != "1"){
            return Result.fail(3011,"签到状态参数status错误");
        }
        return Result.success(msg,count);
    }

    @Override
    public Result queryStudentAttendance(Long courseId){
        List<AttendanceVo> attendanceVos = attendanceMapper.queryStudentAttendance(courseId);
        if (attendanceVos.size() > 0) {
            return Result.success("查询["+courseId+"]课程学生签到信息列表成功",attendanceVos);
        } else {
            return Result.fail(3020,"查询["+courseId+"]课程学生签到信息列表失败");
        }
    }
}
